package com.shwread.android.qysw.step.model;

import android.util.Log;

import com.shwread.android.qysw.step.dto.DaysStepDto;
import com.shwread.android.qysw.tools.DataKeeperHelper;
import com.shwread.android.qysw.tools.TimeHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Fantasy on 16/4/3.
 */
public class UserHealthyDataModel {
    private static final String TAG_USER_DAILY_STEP = "tag_user_daily_step";
    public static final String TAG_HAS_INIT = "tag_has_init";
    public static final String INIT_DAY = "init_day";
    List<DaysStepDto> daysStepDtoList;

    private static class UserHealthyDataModelHolder {
        public final static UserHealthyDataModel sington = new UserHealthyDataModel();
    }

    public static UserHealthyDataModel getInstance() {
        return UserHealthyDataModelHolder.sington;
    }

    private UserHealthyDataModel() {
        tryLoadLocal();
    }

    public void init() {
        boolean hasInit = DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_HAS_INIT, false);
        if (!hasInit) {
            List<DaysStepDto> list = new ArrayList<>();
            for (int i = 0; i <= 366; i++) {
                DaysStepDto stepDto = new DaysStepDto();
                stepDto.setStep(0);
                list.add(i, stepDto);
            }
            DataKeeperHelper.getInstance().getUserDailyStepCache().put(TAG_USER_DAILY_STEP, list);
            Log.i("darren", "list:" + list);
            DataKeeperHelper.getInstance().getUserDailyStepCache().put(TAG_HAS_INIT, true);

            Calendar ca = Calendar.getInstance();//创建一个日期实例
            ca.setTime(new Date());//实例化一个日期
            System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
            int day = ca.get(Calendar.DAY_OF_YEAR);
            DataKeeperHelper.getInstance().getUserDailyStepCache().putInt(INIT_DAY, day);
        }

    }

    private boolean tryLoadLocal() {
        daysStepDtoList = new ArrayList<>();
        daysStepDtoList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);
        return daysStepDtoList != null;
    }

    public List<DaysStepDto> getLastSevenDaysStep(Date date) {
        List<DaysStepDto> SevendaysStepDtoList = null;
        if (tryLoadLocal()) {
            SevendaysStepDtoList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);
        }
        return SevendaysStepDtoList;
    }

    public void submitTodayStep(int step) {
        if (tryLoadLocal()) {
            DaysStepDto todaystepDto = new DaysStepDto();
            todaystepDto.setStep(step);
            Calendar ca = Calendar.getInstance();//创建一个日期实例
            ca.setTime(new Date());//实例化一个日期
            System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
            int day = ca.get(Calendar.DAY_OF_YEAR);
            Log.i("darren", "submit:" + "day:" + day + "step:" + step + "getStep:" + todaystepDto.getStep());
            daysStepDtoList.set(day, todaystepDto);
            DataKeeperHelper.getInstance().getUserDailyStepCache().put(TAG_USER_DAILY_STEP, daysStepDtoList);
        } else {
//            init();
//            DaysStepDto todaystepDto = new DaysStepDto();
//            todaystepDto.setStep(step);
//            Calendar ca = Calendar.getInstance();//创建一个日期实例
//            ca.setTime(new Date());//实例化一个日期
//            System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
//            int day = ca.get(Calendar.DAY_OF_YEAR);
//            daysStepDtoList.add(day, todaystepDto);
        }
    }

    public List<DaysStepDto> getLastSevenDaysData() {
        List<DaysStepDto> allDaysList = new ArrayList<>();
        allDaysList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(new Date());//实例化一个日期
        System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
        int day = ca.get(Calendar.DAY_OF_YEAR);
        List<DaysStepDto> daysStepDtoList = new ArrayList<>();
        int x = 0;
        for (int i = day - 6; i <= day; i++) {
            Log.i("darren", i + ":" + allDaysList.get(i).getStep());
            daysStepDtoList.add(x++, allDaysList.get(i));
        }
        return daysStepDtoList;
    }

    public List<DaysStepDto> getLastFourWeekData() {
        List<DaysStepDto> allDaysList = new ArrayList<>();
        allDaysList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(new Date());//实例化一个日期
        System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
        int day = ca.get(Calendar.DAY_OF_YEAR);
        List<DaysStepDto> daysStepDtoList = new ArrayList<>();
        int x = 0;
        int theWeekDay = TimeHelper.getWeeKDay(new Date());
        Log.i("darren", "week:" + theWeekDay);
        int nowWeek = 0;
        for (int i = day - theWeekDay + 1; i <= day; i++) {
            nowWeek += allDaysList.get(i).getStep();

        }
        DaysStepDto fdaysStepDto = new DaysStepDto();
        fdaysStepDto.setStep(nowWeek);
        daysStepDtoList.add(x++, fdaysStepDto);
        int dd = day - theWeekDay;
        for (int j = 1; j <= 3; j++) {
            int firstW = 0, perFirst = 0;
            for (int i = dd - 7 * j + 1; i <= dd - 7 * (j - 1); i++) {
                firstW += allDaysList.get(i).getStep();
            }
            perFirst = firstW;
            DaysStepDto daysStepDto = new DaysStepDto();
            daysStepDto.setStep(perFirst);
            daysStepDtoList.add(x++, daysStepDto);
        }

        return daysStepDtoList;
    }


    public List<DaysStepDto> getYearData() {
        List<DaysStepDto> allDaysList = new ArrayList<>();
        allDaysList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);
        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(new Date());//实例化一个日期
        System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天
        List<DaysStepDto> daysStepDtoList = new ArrayList<>();
        int x = 0;
        int monthStep = 0;
        for (int j = 0; j < 11; j++) {//月份从0开始
            ca.set(2016, j, 1);
            int startDay = ca.get(Calendar.DAY_OF_YEAR);
            ca.set(2016, j + 1, 1);
            int endDay = ca.get(Calendar.DAY_OF_YEAR);
            monthStep = 0;
            Log.i("darrens", "month:" + j + "startDay:" + startDay + "   endDay:" + endDay);

            for (int i = startDay; i < endDay; i++) {
                monthStep += allDaysList.get(i).getStep();
                Log.i("darren", "i" + allDaysList.get(i).getStep());
            }
            DaysStepDto daysStepDto = new DaysStepDto();
            daysStepDto.setStep(monthStep);
            Log.i("darrens", "monthStep:" + monthStep);
            daysStepDtoList.add(j, daysStepDto);
        }
        ca.set(2016, 11, 1);

        monthStep = 0;
        Log.i("darrens", "lastMonth;" + ca.get(Calendar.DAY_OF_YEAR));
        for (int i = ca.get(Calendar.DAY_OF_YEAR); i <= 365; i++) {
            monthStep += allDaysList.get(i).getStep();
            Log.i("darrens", "monthStep:" + monthStep);
        }
        DaysStepDto daysStepDto = new DaysStepDto();
        daysStepDto.setStep(monthStep);
        daysStepDtoList.add(11, daysStepDto);
        for (int i = 0; i <= 11; i++) {
            Log.i("darreno", "Mstep:" + daysStepDtoList.get(i).getStep());
        }
        return daysStepDtoList;
    }

    public List<DaysStepDto> getAllDatas() {
        List<DaysStepDto> allDaysList = new ArrayList<>();
        allDaysList = (List<DaysStepDto>) DataKeeperHelper.getInstance().getUserDailyStepCache().get(TAG_USER_DAILY_STEP);

        Calendar ca = Calendar.getInstance();//创建一个日期实例
        ca.setTime(new Date());//实例化一个日期
        System.out.println(ca.get(Calendar.DAY_OF_YEAR));//获取是第多少天

        List<DaysStepDto> myDatas = new ArrayList<>();

        int day = ca.get(Calendar.DAY_OF_YEAR);
        int firstDay = DataKeeperHelper.getInstance().getUserDailyStepCache().getInt(INIT_DAY, day);

        List<Date> dateList = TimeHelper.getLastServalDays(day - firstDay + 1);
        int j = 0;
        for (int i = day; i >= firstDay; i--) {
            DaysStepDto daysStepDto = new DaysStepDto();
            daysStepDto = allDaysList.get(i);
            daysStepDto.setDate(dateList.get(j));
            myDatas.add(j, daysStepDto);
            j++;
        }
        return myDatas;
    }
}
